/**
 * Sumpu Inc.
 * Copyright (c) 2011-2012 All Rights Reserved. 
 */
package cn.sumpu.app.guoguojie.web.mvc.account;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.sumpu.app.guoguojie.biz.AccountBiz;
import cn.sumpu.app.guoguojie.biz.AccountDrawApplyBiz;
import cn.sumpu.app.guoguojie.biz.TopBiz;
import cn.sumpu.app.guoguojie.domain.Account;
import cn.sumpu.app.guoguojie.domain.AccountDrawApply;
import cn.sumpu.app.guoguojie.domain.Member;
import cn.sumpu.app.guoguojie.domain.OnLineUserListener;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;
import cn.sumpu.app.guoguojie.util.PageModel;

/**
 * @author nick
 * @version $Id: AccountController.java, v 0.1 2012-8-31 上午10:42:21 nick Exp $
 */
@Controller
public class AccountController {

    private final static Logger logger            = LoggerFactory.getLogger(AccountController.class);

    private final static int    ACCOUNT_PAGE_SIZE = 6;

    @Autowired
    private AccountBiz          accountBiz;
    @Autowired
    private TopBiz              topBiz;
    @Autowired
    private AccountDrawApplyBiz applyBiz;

    @RequestMapping(value = "/account/account.htm")
    public String account(HttpServletRequest request, HttpServletResponse response) {
        try{
            Member member = OnLineUserListener.getSessionMember(request);
            if (Common.empty(member)) {
                return "v3/member/loginRegister/login";
            }
            request.setAttribute("member", member);
        }catch(Exception e){
            e.printStackTrace();
        }
        return "v3/account/account";
    }

    @RequestMapping(value = "/account/accountdetail.htm")
    public String accountDetail(String memberId, HttpServletRequest request, HttpServletResponse response) {
        Member member = OnLineUserListener.getSessionMember(request);
        if (Common.empty(member)) {
            return "v3/member/loginRegister/login";
        }
        try {
            if (!Common.emptyStr(memberId)) {
                Account account = accountBiz.getAccountByMemberId(memberId);
                request.setAttribute("account", account);
                request.setAttribute("memberId", memberId);
                request.setAttribute("member", member);
                Integer applys = applyBiz.getAdaCountByCondition(member.getNickname(), "01");
                request.setAttribute("applys", applys);
            } else {
                throw new AppBusinessException("Request memberId is null");
            }
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("[/account/accountdetail.htm] fail", e);
            }
        }
        return "v3/account/accountDetail";
    }

    @RequestMapping(value = "/account/accountloglist.htm")
    public String accountLogList(String page, String pagesize, String memberId, HttpServletRequest request, HttpServletResponse response) {
        Member member = OnLineUserListener.getSessionMember(request);
        if (Common.empty(member)) {
            return "v3/member/loginRegister/login";
        }
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("page", page);
            if (!Common.emptyStr(pagesize)) {
                params.put("pageSize", pagesize);
            } else {
                params.put("pageSize", ACCOUNT_PAGE_SIZE);
            }
            if (!Common.emptyStr(memberId)) {
                Account account = accountBiz.getAccountByMemberId(memberId);
                if (null != account) {
                    params.put("account", account.getIdentifier());
                } else {
                    throw new AppBusinessException("Can't find member account[memberId=" + memberId + "]");
                }
            } else {
                throw new AppBusinessException("Can't find member[memberId=" + memberId + "]");
            }
            PageModel pm = accountBiz.getAccountLog(params);
            request.setAttribute("pageModel", pm);
            request.setAttribute("member", member);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("[/account/accountloglist.htm] fail", e);
            }
        }
        return "v3/account/accountLogList";
    }

    @RequestMapping(value = "/account/purchaselist.htm")
    public String purchaseList(String page, String pagesize, String memberId, HttpServletRequest request, HttpServletResponse response) {
        Member member = OnLineUserListener.getSessionMember(request);
        if (Common.empty(member)) {
            return "v3/member/loginRegister/login";
        }
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("page", page);
            if (!Common.emptyStr(pagesize)) {
                params.put("pageSize", pagesize);
            } else {
                params.put("pageSize", ACCOUNT_PAGE_SIZE);
            }
            if (!Common.emptyStr(memberId)) {
                params.put("outerCode", memberId);
            }
            PageModel pm = topBiz.getTaobaokeReport(params);
            request.setAttribute("pageModel", pm);
            request.setAttribute("member", member);
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("[/account/purchaselist.htm] fail", e);
            }
        }
        return "v3/account/purchaseList";
    }
    
    //验证提现合法性
    @RequestMapping(value = "/account/woyaotixian.htm")
    public String woyaotixian(HttpServletRequest request, HttpServletResponse response) {
        try{
            response.setContentType("text/html; charset=UTF-8");
            PrintWriter out = response.getWriter();
            Member member = OnLineUserListener.getSessionMember(request);
            if (Common.empty(member)) {
                return null;
            }
            //验证头像和邮箱合法性
            if(Common.emptyStr(member.getAvatar()) || Common.AVATAR_MEMBER_DEFAULT.equals(member.getAvatar()) || !member.getIsActive()){
                out.print("avatar email");
                return null;
            }
            Account account = accountBiz.getAccountByMemberId(member.getIdentifier());
            if(account.getRmb()<50){
                out.print("xiaoyu50");
                return null;
            }else{
                out.print("success");
                return null;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    //打开提现窗口
    @RequestMapping(value = "/account/openwytx.htm")
    public String openwytx(HttpServletRequest request, HttpServletResponse response) {
        try{
            Member member = OnLineUserListener.getSessionMember(request);
            if (Common.empty(member)) {
                return null;
            }
            Account account = accountBiz.getAccountByMemberId(member.getIdentifier());
            request.setAttribute("account", account);
        }catch(Exception e){
            e.printStackTrace();
        }
        return "v3/account/tixian";
    }
    
    //保存 会员提现申请
    @RequestMapping(value = "/account/savetianapply.htm")
    public String saveTianApply(HttpServletRequest request, HttpServletResponse response, AccountDrawApply ada) {
        try{
            response.setContentType("text/html; charset=UTF-8");
            PrintWriter out = response.getWriter();
            Member member = OnLineUserListener.getSessionMember(request);
            if (Common.empty(member)) {
                return null;
            }
            Account account = accountBiz.getAccountByMemberId(member.getIdentifier());
            //验证合法性
            if(!member.getEnabled()|| Common.emptyStr(member.getAvatar()) || Common.AVATAR_MEMBER_DEFAULT.equals(member.getAvatar()) || !member.getIsActive() || account.getRmb()<50
                    || applyBiz.getAdaCountByCondition(member.getNickname(), "01")>0){
                out.print("error");
                return null;
            }
            ada.setAccount(account.getIdentifier());
            ada.setDrawRmb(account.getRmb()-account.getRmb()%50);
            ada.setNickname(member.getNickname());
            ada.setStatus("01");
            applyBiz.addAccountDrawApply(ada);
            out.print("success");
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
}
